<template>
  <div class="ssk">
    <el-container>
      <el-input
        style="width: 20%"
        placeholder="食品编码/名称"
        v-model="parameter.content"
        clearable
      ></el-input>
      <el-button
        type="primary"
        size="mini"
        round
        @click="onSubmit"
        icon="el-icon-search"
        >查询</el-button
      >
    </el-container>
    <el-container>
      <el-button
        type="success"
        size="mini"
        icon="el-icon-circle-plus"
        @click.native="addTmeals()"
        round
        >添加</el-button
      >
    </el-container>
    <el-table
      style="width: 100%"
      align="center"
      v-loading="loading"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      :data="AllUser"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="编码" prop="code" width="120">
      </el-table-column>
      <el-table-column label="食品名称" prop="name" width="120">
      </el-table-column>
      <el-table-column label="所属类型" prop="tmealsType.name" width="120">
      </el-table-column>
      <el-table-column label="100g含能量" prop="energy" width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.energy>0" style="margin-left: 10px"> {{scope.row.energy}} /g</span>
          <span v-if="scope.row.energy==null || scope.row.energy==0" style="margin-left: 10px">--/g</span>
        </template>
      </el-table-column>
      <el-table-column label="蛋白质含量" prop="protein" width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.protein>0" style="margin-left: 10px"> {{scope.row.protein}} /g</span>
          <span v-if="scope.row.protein==null || scope.row.protein==0" style="margin-left: 10px">--/g</span>
        </template>
      </el-table-column>
      <el-table-column label="脂肪含量" prop="fat" width="300">
        <template slot-scope="scope">
          <span v-if="scope.row.fat>0" style="margin-left: 10px"> {{scope.row.fat}} /g</span>
           <span v-if="scope.row.fat==null || scope.row.fat==0" style="margin-left: 10px">--/g</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            round
            @click.native="update(scope.$index, scope.row)"
            icon="el-icon-edit"
          >
            修改
          </el-button>
          <el-button
            type="danger"
            size="mini"
            round
            @click.native="deleteRow(scope.$index, scope.row)"
            icon="el-icon-delete"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="parameter.page"
      :page-sizes="[2, 3, 10, 20]"
      :pager-count="9"
      background
      :page-size.sync="parameter.num"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <el-dialog :visible.sync="dialogFormVisible" width="1000px">
                        <el-form :model="form"  label-width="80px">
                            <el-row>
                                <el-col :span="10" :offset="1"><dh style="color:red">*</dh>食品名称:<el-input v-model="form.name" size="mini" @blur="byid()" :disabled="bj2" /></el-col>
                                <el-col :span="10" :offset="1">
                                    <dh style="color:red">*</dh>所属类别:<el-select v-model="form.typeid" size="mini" style="width: 160px;">
                                        <el-option v-for="(item,index) in tmealstype" :key="index" :label="item.name" :value="item.id"></el-option>
                                    </el-select>
                                </el-col>
                            </el-row>
                            <br/>
                            <el-row>
                                <el-col :span="10" :offset="1" style="margin-left:34px"><dh style="color:red">*</dh>每Kcal能量:<el-input v-model="form.kcal" size="mini"/></el-col>
                                <el-col :span="10" :offset="1" style="margin-left:44px"><dh style="color:red">*</dh>每KJ能力:<el-input v-model="form.kj" size="mini"/></el-col>
                            </el-row>
                            <br/>
                            <el-row>
                                <el-col :span="10" :offset="1" style="margin-left:20px"><dh style="color:red">*</dh>碳水化合物含量:<el-input v-model="form.carbohydrate"  size="mini"/></el-col>
                                <el-col :span="10" :offset="1" style="margin-left:60px"><dh style="color:red">*</dh>脂肪含量:<el-input v-model="form.fat"  size="mini"/></el-col>
                            </el-row>
                            <br/>
                            <el-row>
                                <el-col :span="10" :offset="1" style="margin-left:34px"><dh style="color:red">*</dh>蛋白质含量:<el-input v-model="form.protein"  size="mini"/></el-col>
                               <el-col :span="10" :offset="1" style="margin-left:38px"><dh style="color:red">*</dh>100g含能量:<el-input v-model="form.energy"  size="mini"/></el-col>
                            </el-row>
                            <br/>
                            <br/>
                            <el-row style="margin-left:15px">
                                <el-col :span="2" :offset="2">说明:</el-col>
                                <el-col :span="14"><el-input v-model="form.explains" type="textarea"  :rows="3" size="mini"/></el-col>
                            </el-row>
                        </el-form>
                                <el-row style="margin-top:20px">
                                    <el-col :span="2" :offset="7"><el-button round size="mini" type="primary" @click="updStu()" :disabled="bj1">确 定</el-button></el-col>
                                    <el-col :span="2" :offset="7"><el-button round size="mini" type="info" @click="dialogFormVisible = false">取 消</el-button></el-col>
                                </el-row>
        </el-dialog>
  </div>
</template>

<script>
import Vue from "vue";
import axios from 'axios'
export default Vue.extend({
  data() {
    return {
      
      loading:true,
      AllUser: [],
      tmealstype:[],
      parameter: {
        page: 1,
        num: 3,
        content: "",
      },
      total: 0,
      bj:'',
      bj1:false,
      bj2:false,
      dialogFormVisible : false,
      form:{
        id:'',
        code:'',
        name:'',
        typeid:'',
        energy:'',
        protein:'',
        fat:'',
        carbohydrate:'',
        kcal:'',
        kj:'',
        explains:'',
        createTime:'',
        updateTime:'',
      },
    };
  },
  methods: {
   
    deleteRow(index,row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
           axios.get("/api/lqHealth/delTmeals?id="+row.id).then(data=>{
            if(data.data==1){
            this.load();
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
              }else{
                 this.$message({
                  type: 'info',
                  message: '此检查项已被套餐选择，不能删除!!'
                 }); 
               }
           });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    handleSelectionChange() {},
    handleSizeChange(val) {
      this.parameter.num=val;
      this.load();
    },
    handleCurrentChange(val) {
      this.parameter.page=val;
      this.load();
    },
     byid(){
      axios.get("/api/lqHealth/byCode?name="+this.form.name).then(data=>{
        if(data.data==2){
          this.bj1=true;
        }else{
          this.bj1=false;
        }
      })
    },
    onSubmit() {
      this.load();
    },
    addTmeals() {
      this.form={};
      this.dialogFormVisible = true;
      this.bj=true;
      this.bj2=false;
    },
    update(index,row){
      this.bj1=false;
      this.bj2=true;
      this.form=row;
      this.form.typeid=parseInt(row.typeid)
      this.bj=false;
      this.dialogFormVisible=true;
    },
    updStu(){
      let upd = new Date();
      
      if(this.bj){
        this.form.createTime=upd;
        axios.get("/api/lqHealth/addTmeals?tmeal="+JSON.stringify(this.form)).then((data) => {
            if(data.data==1){
              this.$message({
                message: '恭喜你，添加成功！！！',
                type: 'success'
                });
              this.load();
            }
        });
      }else{
        this.form.updateTime=upd;
        axios.get("/api/lqHealth/updTmeals?tmeal="+JSON.stringify(this.form)).then((data) => {
            if(data.data==1){
              this.$message({
                message: '恭喜你，修改成功！！！',
                type: 'success'
                });
              this.load();
            }
        });
      }
      this.dialogFormVisible = false
    },
    //分页加载
    load() {
      axios
        .post("/api/lqHealth/selectAllTMeals", this.parameter)
        .then((res) => {
          //console.log(res.data);
          this.AllUser = res.data.list;
          this.parameter.page = res.data.pageNum;
          this.parameter.num = res.data.pageSize;
          this.total = res.data.total;

          //加载样式
        setTimeout(() => {
          this.loading=false;
        }, 1000);
        });
    },
    loadType(){
      axios.get("/api/lqHealth/getAll").then(data=>{
          //console.log(data.data)
          this.tmealstype=data.data;
      })
    }
  },
  created() {
    this.load();
    this.loadType();
  },
});
</script>
<style scoped>
.el-input{
  width: 130px;
}
.el-dialog{
  height: 20%;
}
</style>